/*
 * @Author: Crism
 * @Date: 2018-07-16 09:33:00
 * @LastEditors: Crism
 * @LastEditTime: 2018-08-02 17:47:01
 * @Description: 
 */


const HttpUtil = require('../util/HttpUtil');
const rp = require('request-promise');
const CommomUtil = require('../libs/CommomUtil');
const http = require('https');
const data_m = require('../model/data_m');
// const iconv = require('iconv-lite');

module.exports.getData = getData;
module.exports.getDataTest = getDataTest;

async function getData(ctx) {
    let { url } = CommomUtil.getReqData(ctx);
    let data = [];

    let res = await HttpUtil.httpGet(url);
    let ListData = await data_m.getTitleList(res);

    for (let a = 0; a < ListData.length; a++) {
        let res = await HttpUtil.httpGet(ListData[a]['url']);
        let content = await data_m.getDetail(res);
        data.push({
            'title': ListData[a]['title'],
            'content': content
        });
    }

    let resData = {};
    resData['data'] = data;

    return ctx.body = resData;
}

async function getDataTest(ctx) {

    let { url } = CommomUtil.getReqData(ctx);
    let html = await HttpUtil.httpGet(url);
    let data = await data_m.getDataTest(html);
    let resData = {};
    resData['data'] = data;

    return ctx.body = resData;
}